package com.sunny.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sunny.dataobject.OrderDetail;
import com.sunny.dto.OrderDTO;
import com.sunny.enums.ResultEnum;
import com.sunny.exception.SellException;
import com.sunny.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2018/4/9.
 */
@Slf4j
public class OrderForm2OrderDTOConverter {

    public static OrderDTO convert(OrderForm orderForm){
        Gson gson=new Gson();
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        //讲一个字符串转化为list
        List<OrderDetail> orderDetailList=new ArrayList<>();
        try {
            orderDetailList = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>() {
                    }.getType());
        }catch (Exception e){
            log.error("[对象转换] 错误，string={} ",orderForm.getItems() );
            throw  new SellException(ResultEnum.PARAM_ERROR);
        }

        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }

}
